package com.example.aircraftwar.codes.activity;

import androidx.appcompat.app.AppCompatActivity;
import com.example.aircraftwar.R;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;

public class OptActivity extends AppCompatActivity {
    private Socket socket;
    public static PrintWriter writer;
    public static BufferedReader in = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_opt);
    }

    public void single(View view) {
        Intent intent = new Intent();
        intent.setClass(this,MenuActivity.class);
        RankActivity.isConnected = false;
        startActivity(intent);
    }

    public void multiplayer(View view) {
        Intent intent = new Intent();
        intent.setClass(this,MenuActivity.class);
        RankActivity.isConnected = true;
        new Thread(new toConnect()).start();
        startActivity(intent);
    }

    public void goRanking(View view) {
        Intent intent = new Intent();
        intent.setClass(this,MenuActivity.class);
        startActivity(intent);
    }

    protected class toConnect extends Thread{
        @Override
        public void run(){
            try{
                socket = new Socket();
                socket.connect(new InetSocketAddress("10.250.124.200",9999),5000);
                //bufferedInputStream = new BufferedInputStream(socket.getInputStream());
                //bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream());
                writer = new PrintWriter(new BufferedWriter(
                        new OutputStreamWriter(
                                socket.getOutputStream(),"UTF-8")),true);
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                Log.i("client","connect to server");
            } catch(IOException ex){
                ex.printStackTrace();
            }
        }
    }
}
